function Game(){
	this.eng = new Engine(vec2(853, 480), Utils.get("divGame"));
	
	this.getCtx().lineWidth = 2;
	this.getCtx().font = '12px "c64Font"';
	
	this.keys = [];
	this.fps = 1000 / 30;
	this.map = null;
	
	this.images = {};
	this.loadImages();
	
	this.player = new PlayerStats("Kram", 80, 30, 15, 15, 15, 15, 15); 
}

Game.prototype.loadImages = function(){
	this.images["input1"] = this.eng.loadImage("img/input1.png");
	this.images["input2"] = this.eng.loadImage("img/input2.png");
};

Game.prototype.clearScreen = function(){
	var ctx = this.getCtx();
	
	ctx.fillStyle = "black";
	ctx.fillRect(0,0,ctx.width,ctx.height);
};

Game.prototype.drawLine = function(/*Int*/ x1, /*Int*/ y1, /*Int*/ x2, y2){
	this.eng.drawLine(x1,y1,x2,y2);
};

Game.prototype.getCtx = function(){
	return this.eng.ctx;
};

Game.prototype.drawFrame = function(/*Int*/ x,/*Int*/ y,/*Int*/ w,/*Int*/ h){
	var ctx = this.getCtx();
	
	ctx.fillStyle = "rgb(63,72,204)";
	ctx.fillRect(x,y,8,h);
	ctx.fillRect(x+w-8,y,8,h);
	ctx.fillRect(x,y,w,8);
	ctx.fillRect(x,y+h-8,w,8);
	
	ctx.strokeRect(x,y,w,h);
	ctx.strokeRect(x+8,y+8,w - 16,h - 16);
};

Game.prototype.getDirectionName = function(direction){
	switch (direction){
		case 0: return "EAST"; break;
		case 1: return "NORTH"; break;
		case 2: return "WEST"; break;
		case 3: return "SOUTH"; break;
	}
};

Game.prototype.drawPlayerStats = function(){
	var ctx = this.getCtx();
	
	ctx.fillStyle = "white";
	
	var r = this.map.renderSize;
	var x = r.a + 20;
	var p = this.player;
	
	ctx.fillText(p.name, x, 30);
	ctx.fillText("LVL: " + p.lvl, x, 45);
	ctx.fillText("EXP: " + p.exp, x, 60);
	ctx.fillText("HP: " + p.hp + " / " + p.mHp, x, 75);
	ctx.fillText("MP: " + p.mp + " / " + p.mMp, x, 90);
	ctx.fillText("HGRY: " + p.stm + " / 100", x, 105);
	ctx.fillText("STR: " + p.str, x, 120);
	ctx.fillText("DFS: " + p.dfs, x, 135);
	ctx.fillText("SPD: " + p.spd, x, 150);
	ctx.fillText("WIS: " + p.wis, x, 165);
	ctx.fillText("LUK: " + p.luk, x, 180);
	
	ctx.fillText("WEAPON: ", x, 210);
	var w = (p.weapon != null)? p.weapon.name : "Hands";
	ctx.fillText(w, x + 20, 225);
	
	ctx.fillText("ARMOUR: ", x, 240);
	var a = (p.armour != null)? p.armour.name : "Skin";
	ctx.fillText(a, x + 20, 255);
	
	ctx.fillText("SPELL : ", x, 270);
	var s = (p.spell != null)? p.spell.name : "None";
	ctx.fillText(s, x + 20, 285);
};

Game.prototype.drawInterface = function(){
	var r = this.map.renderSize;
	var w = (853-r.a);
	var h = 480-r.b;
	var ctx = this.getCtx();
	
	
	this.drawFrame(0,0,r.a,r.b);
	
	// Console
	ctx.fillStyle = "black";
	ctx.fillRect(200,r.b,653-w,h);
	this.drawFrame(200,r.b,653-w,h);
	var c = 0;
	for (var i=Console.messages.length-1;i>=0;i--){
		var msg = Console.messages[i];
		var m = msg.msg;
		if (msg.amount > 1) m += " (x" + msg.amount + ") ";
		
		ctx.fillStyle = msg.color;
		ctx.fillText(m, 216, 464 - (c++ * 14));
	}
	
	// Left Input
	ctx.fillStyle = "black";
	ctx.fillRect(0,r.b,200,h);
	this.drawFrame(0,r.b,200,h);
	ctx.fillStyle = "white";
	ctx.textAlign = "center";
	ctx.fillText(this.map.name,100,r.b + 26);
	ctx.fillText(this.getDirectionName(this.map.player.direction),100,465);
	ctx.drawImage(this.images["input1"], 25, 345);
	ctx.textAlign = "left";
	
	// Player Info
	ctx.fillStyle = "black";
	ctx.fillRect(r.a,0,w,r.b);
	this.drawFrame(r.a,0,w,r.b);
	this.drawPlayerStats();
	
	//Right Input
	ctx.fillStyle = "black";
	ctx.fillRect(r.a,r.b,w,480-r.b);
	this.drawFrame(r.a,r.b,w,480-r.b);
	ctx.drawImage(this.images["input2"], 654, 335);
};

Game.prototype.getKeyPressed = function(keyCode){
	if (this.keys[keyCode] == 1){
		this.keys[keyCode] = 2;
		return true;
	}
	
	return false;
};

Game.prototype.mapTouch = function(/*Int*/ x, /*Int*/ y){
	//25, 345
	var game = this;
	if (y >= 395 && y <= 445){
		if (x >= 25 && x < 75){
			game.keys[37] = 1;
			setTimeout(function(){ game.keys[37] = 0; }, 50);
		}else if (x >= 75 && x < 125){
			game.keys[40] = 1;
			setTimeout(function(){ game.keys[40] = 0; }, 50);
		}else if (x >= 125 && x < 175){
			game.keys[39] = 1;
			setTimeout(function(){ game.keys[39] = 0; }, 50);
		}
	}else if (y >= 345 && y < 395){
		if (x >= 75 && x < 125){
			game.keys[38] = 1;
			setTimeout(function(){ game.keys[38] = 0; }, 50);
		}
	}
	
	if (y >= 335 && y < 385){
		if (x >= 654 && x < 704){
			game.keys[65] = 1;
			setTimeout(function(){ game.keys[65] = 0; }, 50);
		}
	}else if (y >= 404 && y < 454){
		if (x >= 654 && x < 704){
			game.keys[76] = 1;
			setTimeout(function(){ game.keys[76] = 0; }, 50);
		}
	}
};

Game.prototype.newGame = function(){
	if (this.eng.areImagesReady()){
		this.map = new MapManager(this);
		this.loopGame();
	}else{
		var game = this;
		setTimeout(function(){ game.newGame(); });
	}
};

Game.prototype.loopGame = function(){
	var game = this;
	
	game.map.loop();
	this.drawInterface();
	
	setTimeout(function(){ game.loopGame(); }, game.fps);
};

Utils.addEvent(window, "load", function(){
	var game = new Game();
	
	game.newGame();
	
	Utils.addEvent(document, "keydown", function(e){
		if (window.event) e = window.event;
		
		if (game.keys[e.keyCode] == 2) return;
		
		game.keys[e.keyCode] = 1;
	});
	
	Utils.addEvent(document, "keyup", function(e){
		if (window.event) e = window.event;
		
		game.keys[e.keyCode] = 0;
	});
	
	var isMobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));
	var touchE = (isMobile)? "touchstart" : "mousedown";
	var canvas = game.eng.canvas;
	Utils.addEvent(canvas, touchE, function(e){
		if (window.event) e = window.event;
		
		var x, y, scale;
		scale = canvas.offsetHeight / 480;
		
		if (isMobile){
			x = e.touches.item(0).clientX;
			y = e.touches.item(0).clientY;
		}else{
			x = e.clientX;
			y = e.clientY;
		}
		
		x /= scale;
		y /= scale;
		
		game.mapTouch(x, y);
	});
});
